package team.seekers.skill.controller;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import team.seekers.skill.common.utils.ParamCheckUtils;
import team.seekers.skill.entity.dao.SectionBlog;
import team.seekers.skill.entity.dao.SectionVideo;
import team.seekers.skill.entity.vo.admin.add.AdminBlogAddVO;
import team.seekers.skill.entity.vo.admin.add.AdminVideoAddVO;
import team.seekers.skill.entity.vo.admin.update.AdminBlogUpdateVO;
import team.seekers.skill.entity.vo.admin.update.AdminVideoUpdateVO;
import team.seekers.skill.result.Result;
import team.seekers.skill.result.ResultCode;
import team.seekers.skill.service.SectionBlogService;
import team.seekers.skill.service.SectionVideoService;

import java.util.List;

@RestController
@RequestMapping("/api/skill/video")
@Validated
public class SectionVideoController {

    @Autowired
    private SectionVideoService sectionVideoService;

    @PostMapping("/add")
    @PreAuthorize("hasRole('ROLE_ADMIN')")
    public Result add(@RequestBody AdminVideoAddVO adminVideoAddVO){
        if(ParamCheckUtils.isNotRightId(adminVideoAddVO.getSectionId())){
            return Result.fail(ResultCode.PARAM_NOT_VALID);
        }
        SectionVideo sectionVideo = new SectionVideo();
        BeanUtils.copyProperties(adminVideoAddVO,sectionVideo);
        boolean save = sectionVideoService.save(sectionVideo);
        if(!save){
            return Result.fail(ResultCode.ACTION_FAIL);
        }
        return Result.success();
    }

    @DeleteMapping("/delete/{videoId}")
    @PreAuthorize("hasRole('ROLE_ADMIN')")
    public Result delete(@PathVariable("videoId") String videoId){
        if(ParamCheckUtils.isNotRightId(videoId)){
            return Result.fail(ResultCode.PARAM_NOT_VALID);
        }
        boolean remove = sectionVideoService.removeById(videoId);
        if(!remove){
            return Result.fail(ResultCode.ACTION_FAIL);
        }
        return Result.success();
    }

    @PutMapping("/update")
    @PreAuthorize("hasRole('ROLE_ADMIN')")
    public Result update(@RequestBody AdminVideoUpdateVO adminVideoUpdateVO){
        if(ParamCheckUtils.isNotRightId(adminVideoUpdateVO.getId())){
            return Result.fail(ResultCode.PARAM_NOT_VALID);
        }
        SectionVideo sectionVideo = sectionVideoService.getById(adminVideoUpdateVO.getId());
        BeanUtils.copyProperties(adminVideoUpdateVO,sectionVideo);
        boolean updateById = sectionVideoService.updateById(sectionVideo);
        if(!updateById){
            return Result.fail(ResultCode.ACTION_FAIL);
        }
        return Result.success();
    }

    @GetMapping("/listUpdateVOBySectionId/{sectionId}")
    @PreAuthorize("hasRole('ROLE_ADMIN')")
    public Result listBySectionId(@PathVariable("sectionId") String sectionId){
        if(ParamCheckUtils.isNotRightId(sectionId)){
            return Result.fail(ResultCode.PARAM_NOT_VALID);
        }
        List<AdminVideoUpdateVO> adminVideoUpdateVOList = sectionVideoService.listAdminVideoUpdateVOBySectionId(sectionId);
        if(adminVideoUpdateVOList == null){
            return Result.fail(ResultCode.GET_FAIL);
        }
        return Result.success("rows",adminVideoUpdateVOList);
    }
}
